//生成uuid
export function generateUUID() {
	var d = new Date().getTime();
	if (window.performance && typeof window.performance.now === "function") {
		d += performance.now(); //use high-precision timer if available
	}
	var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		var r = (d + Math.random() * 16) % 16 | 0;
		d = Math.floor(d / 16);
		return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
	});
	return uuid;
}

// 随机生成16位数id
export function randomeId() {
	const num1 = new Date()
		.getTime()
		.toString()
		.slice(-8);
	const num2 = Math.floor(Math.random() * 8 * 10).toString();
	const id = Number(num1 + num2);
	return id;
}

//ws发送消息
export function wsSend(ws, data) {
	let msgBody = {
		type:data.contentType,
		sTime:'',
		serial:'',
		content: data.content
	}
	const txt = JSON.stringify(msgBody)
	const params = {
		data: txt
	}
	console.log('params',params)
	ws.send(params)
}

//  返回消息消息体格式
export function msgBody(data) {
	return {
		meetingId: data.meetingId,
		user: {
			memberId: data.memberId,
			memberType: "member"
		},
		kf: {
			waiterId: data.waiterId,
			waiterType: "waiter"
		},
		contentType: data.contentType || 8,
		content: data.content || ''
	}
}

//显示下聊天窗口的文本消息体封装
export function messageBody(params) {
	return {
		type: params.type, //'system'| 'user'
		msg: {
			id: params.id, //消息id（主键） 随机生成
			type: params.talkType || "text", //"text"|"voice"|"img"
			time: params.time,
			userinfo: {
				uid: params.uid , //用户id
				username: "客服小哥",
				// face: "/static/img/face.jpg",  //用户图像
			},
			content:params.content
			// content: {
			// 	text: txt, //key为text表示文本, 为url表示图片或语音地址
			// },
		},
	}

}


/* 类型判断 */
export function checkType(data) { // 用来判断被拷贝对象数据类型的函数
	return Object.prototype.toString.call(data).slice(8, -1)
}
/**
 * @param {any} source
 * @returns {Object}
 */

export function deepClone(source) { // 实现深度拷贝的函数
	let result, sourceType = checkType(source)
	if (sourceType === 'Object') { // 只有对象和数组才进行深度拷贝
		result = {}
	} else if (sourceType === 'Array') {
		result = []
	} else {
		return source
	}
	for (let i in source) { // i为对象的key或数组的下标
		let value = source[i]
		let valueType = checkType(value)
		if (valueType === 'Object' || valueType === 'Array') {
			// 当拷贝的对象中还有数组或是对象时进行递归，克隆它们中的内容，直到递归完所有数组或对象为止
			result[i] = deepClone(value)
		} else {
			result[i] = value
		}
	}
	return result // 返回最终的结果
}

/**
 * @param {Object} FirstOBJ,SecondOBJ
 * @returns {Object}
 */
export function deepAssign(FirstOBJ, SecondOBJ) { // 深度合并对象
	for (var key in SecondOBJ) {
		FirstOBJ[key] = FirstOBJ[key] && FirstOBJ[key].toString() === "[object Object]" ?
			deepAssign(FirstOBJ[key], SecondOBJ[key]) : FirstOBJ[key] = SecondOBJ[key];
	}
	return FirstOBJ;
}


// 历史查询时间格式化 (yyyy-mm-ddTHH:mm:ss)
// 返回[当前时间，一年后的当前时间]
export function recoderTimeformat(date) {
  if (!date || !(date instanceof Date)) {
    date = new Date();
  }
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  const start = `${year-1}-${month}-${day}T${hour}:${minute}:${second}`;
  const end = `${year}-${month}-${day}T${hour}:${minute}:${second}`;
  return [start, end];
}